package com.bj58.analysis.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.bj58.analysis.tools.common.GetObjectToClassUtils;
import com.bj58.analysis.tools.common.InfoDataUtils;
import com.bj58.analysis.tools.common.TrackUrlUtils;
import com.bj58.analysis.tools.common.URLDecodeUtils;
import com.bj58.analysis.tools.common.bean.InfoData;
import com.bj58.analysis.tools.common.bean.LogBean;
import com.bj58.analysis.tools.etl.bean.ETLListShowBean;

/**
 * @description 对listshow ETL后的日志进行解析封装
 * @author tzc
 *
 */
public class ETLListShowAnalysis extends CommonAnalysis {

	@Override
	protected LogBean analysis(String logLine) {
		ETLListShowBean bean = null;
		if (StringUtils.isNotBlank(logLine)) {
			String[] lines = logLine.split("\001", -1);
			bean = (ETLListShowBean) GetObjectToClassUtils.getObjectByArray(
					ETLListShowBean.class, lines);
			if (bean != null) {

				// referer
				String referer = bean.getReferrer();
				if (StringUtils.isNotBlank(referer)) {
					String refDomain = URLDecodeUtils.getSearchEngines(referer);
					if (StringUtils.isNotBlank(refDomain)) {
						// refdomain
						bean.setRefDomain(refDomain);
					}
				}

				// 解析urlParams
				String urlParams = bean.getUrlParams();
				Map<String, String> map = URLDecodeUtils
						.parserCommonParams(urlParams);
				if (map != null && map.size() != 0) {
					// clickTag
					String clickTag = map.get("from");
					if (StringUtils.isNotBlank(clickTag)) {
						clickTag = URLDecodeUtils.parser(clickTag);
						if (StringUtils.isNotBlank(clickTag))
							bean.setClickTag(clickTag);
					}

					// clickId
					String clickId = map.get("ClickID");
					if (StringUtils.isNotBlank(clickId))
						bean.setClickId(clickId);

					// TrackUrl
					String trackURL = map.get("trackURL");
					
					if (StringUtils.isNotBlank(trackURL)) {
						JSONObject obj = null;
						try {
							trackURL = TrackUrlUtils.formatTrackUrl(trackURL);// 到后期可以删除
							obj = JSONObject.fromObject(trackURL);
						} catch (Exception e) {
							e.printStackTrace();
						}// 以上解析方式能正常解析99%的数据，对于极少的个别错误数据直接抛弃trackurl

						if (obj != null) {
							// init_refer
							if (obj.containsKey("init_refer")) {
								String init_refer = obj.getString("init_refer");
								if (StringUtils.isNotBlank(init_refer)) {
									init_refer = URLDecodeUtils
											.parser(init_refer);
									if (StringUtils.isNotBlank(init_refer))
										obj.put("init_refer", init_refer);
								}
							}

							// GTID
							if (obj.containsKey("GTID")) {
								String gTID = obj.getString("GTID");
								if (StringUtils.isNotBlank(gTID))
									bean.setGTID(gTID);
							}

							bean.setTrackUrl(obj.toString());
						}
					}

					// infoDataList
					String infoDataListLine = map.get("info_data");
					if (StringUtils.isNotBlank(infoDataListLine)) {
						String[] infoDataListLines = infoDataListLine.split(
								",", -1);
						List<InfoData> list = new ArrayList<InfoData>();
						for (String infoDataLine : infoDataListLines) {
							InfoData infoData = InfoDataUtils
									.parserToInfoData(infoDataLine);
							if (infoData != null)
								list.add(infoData);
						}
						if (list != null && list.size() != 0)
							bean.setInfoDataList(list);
					}
				}

				// url
				String url = bean.getUrl();
				if (StringUtils.isNotBlank(url)) {

					// PGTID
					String pGTID = URLDecodeUtils.getUrlOfParam(url, "PGTID");
					if (StringUtils.isNotBlank(pGTID))
						bean.setGTID(pGTID);

					// pClickID
					String pClickID = URLDecodeUtils.getUrlOfParam(url,
							"ClickID");
					if (StringUtils.isNotBlank(pClickID))
						bean.setpClickId(pClickID);
				}

			}

		}
		return bean;
	}

}
